<html>
<head><meta charset="utf-8"><title>repr packed · t-compiler/wg-rfc-2229 · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/index.html">t-compiler/wg-rfc-2229</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html">repr packed</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="228151772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228151772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228151772">(Feb 28 2021 at 19:40)</a>:</h4>
<p>What should happen in this piece of code</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[repr(packed)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">X</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="n">s</span>: <span class="nb">String</span><span class="p">,</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>


<span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span>: <span class="nc">X</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="w">   </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">s</span><span class="p">);</span><span class="w"> </span><span class="c1">// by ref</span>
<span class="w">   </span><span class="kd">let</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">s</span><span class="w"> </span><span class="c1">// by move</span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>Do we here look at the overall analysis and since  x.s will be moved (and hence safe to do), and offer that precision or go with since one of the uses of this captured place would be an unsafe access truncate it?</p>



<a name="228152653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228152653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228152653">(Feb 28 2021 at 19:56)</a>:</h4>
<p>I'm leaning towards not truncating, but just want to checl</p>



<a name="228283376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228283376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228283376">(Mar 01 2021 at 18:38)</a>:</h4>
<p>I'm a bit confused</p>



<a name="228283393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228283393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228283393">(Mar 01 2021 at 18:38)</a>:</h4>
<p>access to <code>s</code> by ref is unsafe, right?</p>



<a name="228283406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228283406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228283406">(Mar 01 2021 at 18:39)</a>:</h4>
<p>is there a missing <code>unsafe</code> keyword in this example?</p>



<a name="228283485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228283485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228283485">(Mar 01 2021 at 18:39)</a>:</h4>
<p>hmm, ok, I'm starting to see</p>



<a name="228283512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228283512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228283512">(Mar 01 2021 at 18:39)</a>:</h4>
<p>the constructed closure would contain <code>x_s: x.s</code>, which is a move and hence legal</p>



<a name="228283567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228283567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228283567">(Mar 01 2021 at 18:40)</a>:</h4>
<p>and <em>then</em> the by-ref access (<code>&amp;x.s</code>) would desugar to <code>&amp;self.x_s</code>, which is also legal</p>



<a name="228283633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228283633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228283633">(Mar 01 2021 at 18:40)</a>:</h4>
<p>it feels a bit surprising to me that moving <code>x.s</code> makes this code work</p>



<a name="228321627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228321627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228321627">(Mar 01 2021 at 22:56)</a>:</h4>
<p>Yeah, what's interesting is it doesn't throw a warning even today with the feature: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=fae32d74129205011cd027ee7c04ae76">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=fae32d74129205011cd027ee7c04ae76</a></p>



<a name="228321745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189812-t-compiler/wg-rfc-2229/topic/repr%20packed/near/228321745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aman Arora <a href="https://rust-lang.github.io/zulip_archive/stream/189812-t-compiler/wg-rfc-2229/topic/repr.20packed.html#228321745">(Mar 01 2021 at 22:57)</a>:</h4>
<p>and it doesn't throw a warning even if I  disable the feature an go to stable, i looked at the capture analysis and that looks correct</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>